package com.eol.admin.modules.business.content;

import com.eol.admin.modules.business.AdminCommonUtils;
import com.eol.admin.modules.logging.log.Log;
import com.eol.common.constants.BaseCommonConstants;
import com.eol.common.sequence.SequenceGen;
import com.eol.entity.po.content.ZCourse;
import com.eol.mapper.config.tkMybatis.TableData;
import com.eol.service.content.ZCourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2020/6/10
 * Time: 6:22 PM
 * Description: ZCourseController
 */
@RestController
@RequestMapping("api/course")
public class ZCourseController {

    @Resource
    private SequenceGen sequenceGen;
    @Resource
    private ZCourseService zCourseService;
    @Resource
    private AdminCommonUtils adminCommonUtils;

    @GetMapping
    public ResponseEntity<Object> list(@RequestParam(value = "name", required = false) String name,
                                       @RequestParam(value = "id", required = false) String id,
                                       @RequestParam(value = "merchantId", required = false) String merchantId,
                                       @RequestParam(value = "page", defaultValue = BaseCommonConstants.PAGE_HELP_PAGE) Integer page,
                                       @RequestParam(value = "size", defaultValue = BaseCommonConstants.PAGE_HELP_LIMIT) Integer limit) {
        List<String> merchantList = adminCommonUtils.getSelectMerchant();
        PageHelper.startPage(page + 1, limit);
        List<ZCourse> list = zCourseService.getList(ZCourse
                .builder()
                .name(name)
                .merchantIdList(merchantList)
                .id(id)
                .merchantId(merchantId)
                .build());
        PageInfo<ZCourse> pageInfo = new PageInfo<>(list);
        TableData<ZCourse> tableData = new TableData<>(pageInfo.getTotal(), list);
        PageHelper.clearPage();
        return new ResponseEntity<>(tableData, HttpStatus.OK);
    }

    @PostMapping
    @Log("添加课程")
    @PreAuthorize("@el.check({'course:add'})")
    public ResponseEntity<Object> add(@Valid @RequestBody ZCourse zCourse) {
        String id = sequenceGen.genCourseId();
        String merchantId = adminCommonUtils.getUserMerchantId();
        zCourse.setMerchantId(merchantId);
        zCourse.setId(id);
        zCourse.setCreateTime(new Date());
        zCourse.setUpdateTime(new Date());
        return new ResponseEntity<>(zCourseService.insertSelective(zCourse), HttpStatus.OK);
    }

    @PutMapping
    @Log("修改课程")
    @PreAuthorize("@el.check({'course:edit'})")
    public ResponseEntity<Object> edit(@Valid @RequestBody ZCourse zCourse) {
        ZCourse old = zCourseService.selectById(zCourse.getId());
        adminCommonUtils.validateMerchant(old.getMerchantId());
        zCourse.setCreateTime(new Date());
        zCourse.setUpdateTime(new Date());
        return new ResponseEntity<>(zCourseService.insertSelective(zCourse), HttpStatus.OK);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @Log("删除课程")
    @PreAuthorize("@el.check({'course:del'})")
    public ResponseEntity<Object> del(@PathVariable("id") String id) {
        ZCourse zCourse = zCourseService.selectById(id);
        adminCommonUtils.validateMerchant(zCourse.getMerchantId());
        return new ResponseEntity<>(zCourseService.deleteById(id), HttpStatus.OK);
    }
}
